package org.example.demowebsrocket.controller;

import org.example.demowebsrocket.handler.WebSocketHandler;
import org.example.demowebsrocket.model.Message;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * 消息中心控制器
 *
 * @author zhaokf
 * @date 13:00 2025/8/17
 */
@RestController
@RequestMapping("/message")
public class MessageController {
    private final WebSocketHandler webSocketHandler;

    public MessageController(WebSocketHandler webSocketHandler) {
        this.webSocketHandler = webSocketHandler;
    }

    @PostMapping("/send")
    public ResponseEntity<String> send(@RequestBody Message message) {
        webSocketHandler.sendMessageToUser(message.getTargetUsername(), message);
        return ResponseEntity.ok("消息已送达:" + message.getTargetUsername());
    }
}
